import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_plugin_gdmap/amap_polygon/amap_polygon_view.dart';
import 'package:flutter_plugin_gdmap/amap_polygon/amap_polygon_controller.dart';
import 'package:flutter_plugin_gdmap/model/lat_lng.dart';

import 'bean/polygon_entity.dart';

class AMapPoPage extends StatefulWidget {
  @override
  _AMapPoPageState createState() => _AMapPoPageState();
}

class _AMapPoPageState extends State<AMapPoPage> {

  AMapPolygonController _controller;

  bool isget =false;

    @override
      void initState() {
        super.initState();
        // put();
      }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        resizeToAvoidBottomInset: false,
        appBar: AppBar(
          title: Text('围栏地图'),
        ),
        body: ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: Stack(
            alignment: Alignment.center, //指定未定位或部分定位widget的对齐方式
            children: <Widget>[
              AMapPolygonView(
                onAMapViewCreated: (controller){
                  setState(() {
                    _controller=controller;
                  });
                },
              ),
              Positioned(
                top: 20,
                child: new ElevatedButton(
                    onPressed: put,
                    child: Text("切换")),
              ),
              Positioned(
                top: 70,
                child: new ElevatedButton(
                    onPressed: select,
                    child: Text("选中")),
              ),
            ],
          ),
        ));
  }


  void put(){
    List list = [[[{"Q":30.671127,"R":103.85768100000001,"longitude":103.857681,"latitude":30.671127},{"Q":30.662858,"R":104.023506,"longitude":104.023506,"latitude":30.662858},{"Q":30.672013,"R":103.96617100000003,"longitude":103.966171,"latitude":30.672013},{"Q":30.746103,"R":103.90505999999999,"longitude":103.90506,"latitude":30.746103},{"Q":30.671127,"R":103.85768100000001,"longitude":103.857681,"latitude":30.671127}],[{"Q":30.711541,"R":103.89685500000002,"longitude":103.896855,"latitude":30.711541},{"Q":30.724085,"R":103.904923,"longitude":103.904923,"latitude":30.724085},{"Q":30.712279,"R":103.91796899999997,"longitude":103.917969,"latitude":30.712279},{"Q":30.711541,"R":103.89685500000002,"longitude":103.896855,"latitude":30.711541}]]];
    Map data = {"polygonList": list};
    Map<String, String> map = {
      "list": json.encode(data),
      "location": json.encode(new LatLng(latitude: 30.68900214, longitude: 103.90422821))
    };
    _controller.drawPolygon(map);


  }

  void select() {
    List list = [[[{"Q":30.671127,"R":103.85768100000001,"longitude":103.857681,"latitude":30.671127},{"Q":30.662858,"R":104.023506,"longitude":104.023506,"latitude":30.662858},{"Q":30.672013,"R":103.96617100000003,"longitude":103.966171,"latitude":30.672013},{"Q":30.746103,"R":103.90505999999999,"longitude":103.90506,"latitude":30.746103},{"Q":30.671127,"R":103.85768100000001,"longitude":103.857681,"latitude":30.671127}],[{"Q":30.711541,"R":103.89685500000002,"longitude":103.896855,"latitude":30.711541},{"Q":30.724085,"R":103.904923,"longitude":103.904923,"latitude":30.724085},{"Q":30.712279,"R":103.91796899999997,"longitude":103.917969,"latitude":30.712279},{"Q":30.711541,"R":103.89685500000002,"longitude":103.896855,"latitude":30.711541}]]];
    Map data = {"polygonList": list};
    Map<String, String> map = {
      "list": json.encode(data),
    };
    _controller.polygonInside(map);
  }

}
